package com.DAOTest;

import junit.framework.Assert;
import junit.framework.TestCase;
import java.util.Random;

import com.beans.Movie;
import com.bussiness.MovieBussiness;
import com.dao.moviedao;
import com.exceptions.*;
import com.utility.DBConnection;


public class MovieDAOTest extends TestCase 
{	
	/*
	 * check for valid category Ids
	 * it validates result with 5 category ids.
	 */
	public void testGetCategoryName_valid()
	{
		DBConnection dbcon = new DBConnection();

		String CategoryName="";
		String[] ActualResult = {"Action", "Comedy", "Family", "History", "Mystry"};
		
		int[] categoryId = new int[5];
		for(int i=0;i<5;i++)
		{
			categoryId[i] = i+1;
		}
		
		moviedao bussObj = new moviedao();
	
		for(int i=0;i<5;i++)
		{
			try
			{
				CategoryName = bussObj.getCategoryName(categoryId[i]);
			}
			catch(ServiceUnavailableException suex)
			{
				suex.printStackTrace();
				System.out.println(suex.toString());
			}
			catch(InvalidCategoryIdException invalidex)
			{
				invalidex.printStackTrace();
				System.out.println(invalidex.toString());
			}
			Assert.assertEquals(ActualResult[i], CategoryName);
		}
	}
	
	/*
	 * test for invalid entries, it will throw exception!!!
	 */
	public void testGetCategoryName_invalid()
	{
		int[] categoryId = new int[5];
		int j= 1000;
		for(int i=0;i<5;i++)
		{
			categoryId[i] = j+1;
		}
		
		moviedao bussObj = new moviedao();
	
		for(int i=0;i<5;i++)
		{
			try
			{
				String CategoryName = "";
				CategoryName = bussObj.getCategoryName(categoryId[i]);
			}
			catch(ServiceUnavailableException suex)
			{
				System.out.println(suex.toString());
			}
			catch(InvalidCategoryIdException invalidex)
			{
				System.out.println(invalidex.toString());
			}
		}
	}

	/*
	 * This will return movie depending on the Category Id
	 * Result is tested with argument categoryId = 1;
	 * Category Id is valid category Id from the database!
	 */
	public void testGetMoviesbyCategory_valid() 
	{
		int categoryId=1;
		
		Movie[] movieList= null;
		moviedao bussObj = new moviedao();
		try
		{
			movieList = bussObj.getCategoryMovies(categoryId);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());	
		}
		
		for (int i=0;i<movieList.length;i++)
		{
			Assert.assertEquals(1, movieList[i].getCategorytid());
		}
		
	}
	
	/*
	 * This will return movie depending on the Category Id
	 * Result is tested with argument categoryId = 10000;
	 * Category Id is invalid category Id from the database!
	 */
	public void testGetMoviesbyCategory_inValid() 
	{
		int categoryId=1000;
		
		Movie[] movieList= null;
		moviedao bussObj = new moviedao();
		try
		{
			movieList = bussObj.getCategoryMovies(categoryId);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());	
		}
		
		for (int i=0;i<movieList.length;i++)
		{
			Assert.assertEquals(1, movieList[i].getCategorytid());
		}
		
	}

	/*
	 * Create a movie with valid arguments!
	 */
	public void testCreateMovie_validArguments() {

		int isValid = 0;
		
		int movieIndex = new Random().nextInt( 1000000 );
		Movie newMovie = new Movie();
		newMovie.setMovieName("New Movie" + movieIndex);
		newMovie.setMovieBanner("NewMovie Banner");
		newMovie.setReleaseDate("2011-11-11");
		newMovie.setRent(20);
		newMovie.setCategoryid(1);
		newMovie.setAvailableCopy(10);
		newMovie.setTotalCopy(10);
		newMovie.setNo_of_ratings(0);
		newMovie.setRating(0);
		
		moviedao bussObj = new moviedao();
		try
		{
			isValid = bussObj.createMovie(newMovie);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());		
		}
		catch(MovieAlreadyExistsException ex1)
		{
			System.out.println(ex1.toString());
		}
		catch(InvalidEnteriesException ex2)
		{
			System.out.println(ex2.toString());
		}
		Assert.assertEquals(1, isValid);
	}
	
	/*
	 * Create a movie with invalid field arguments!
	 * It will throw exception invalid Entries!
	 */
	public void testCreateMovie_inValidArguments_1() {

		int isValid = 0;
		
		int movieIndex = new Random().nextInt( 1000000 );
		Movie newMovie = new Movie();
		newMovie.setMovieName("New Movie" + movieIndex);
		newMovie.setMovieBanner("NewMovie Banner");
		newMovie.setReleaseDate("19/19/2011 invalid date field");
		newMovie.setRent(20);
		newMovie.setCategoryid(1);
		newMovie.setAvailableCopy(10);
		newMovie.setTotalCopy(10);
		newMovie.setNo_of_ratings(0);
		newMovie.setRating(0);
		
		moviedao bussObj = new moviedao();
		try
		{
			isValid = bussObj.createMovie(newMovie);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());		
		}
		catch(MovieAlreadyExistsException ex1)
		{
			System.out.println(ex1.toString());
		}
		catch(InvalidEnteriesException ex2)
		{
			System.out.println(ex2.toString());
		}
		Assert.assertEquals(0, isValid);
	}
	
	/*
	 * Create a movie which is already there in the database!
	 * It will throw exception: MovieAlreadyExistsException
	 */
	public void testCreateMovie_inValidArguments_2() {

		int isValid = 0;
		
		int movieIndex = new Random().nextInt( 1000000 );
		Movie newMovie = new Movie();
		newMovie.setMovieName("Immortals");
		newMovie.setMovieBanner("Dir: Tarsem Singh With: Henry ");
		newMovie.setReleaseDate("2011-10-06");
		newMovie.setRent(20);
		newMovie.setCategoryid(1);
		newMovie.setAvailableCopy(10);
		newMovie.setTotalCopy(10);
		newMovie.setNo_of_ratings(0);
		newMovie.setRating(0);
		
		moviedao bussObj = new moviedao();
		try
		{
			isValid = bussObj.createMovie(newMovie);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());		
		}
		catch(MovieAlreadyExistsException ex1)
		{
			System.out.println(ex1.toString());
		}
		catch(InvalidEnteriesException ex2)
		{
			System.out.println(ex2.toString());
		}
		Assert.assertEquals(0, isValid);
	}

	/*
	 * Test update movie with valid movie field arguments!
	 */
	public void testUpdateMovie_valid() 
	{
		int isValid = 0;
		
		int movieIndex = new Random().nextInt( 1000000 );
		Movie newMovie = new Movie();
		newMovie.setMovieId(15);
		newMovie.setMovieName("New Movie" + movieIndex);
		newMovie.setMovieBanner("NewMovie Banner");
		newMovie.setReleaseDate("2011-11-11");
		newMovie.setRent(20);
		newMovie.setCategoryid(1);
		newMovie.setAvailableCopy(0);
		newMovie.setTotalCopy(10);
		newMovie.setNo_of_ratings(0);
		newMovie.setRating(0);
		
		moviedao bussObj = new moviedao();
		try
		{
			isValid = bussObj.updateMovie(newMovie);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());		
		}
		catch(MovieAlreadyExistsException ex1)
		{
			System.out.println(ex1.toString());
		}
		catch(InvalidEnteriesException ex2)
		{
			System.out.println(ex2.toString());
		}
		Assert.assertEquals(1, isValid);
	}
	
	
	/*
	 * update a movie with invalid field arguments!
	 * It will throw exception invalid Entries!
	 */
	public void testUpdateMovie_inValid_1() {

		int isValid = 0;
	
		Movie editMovie = new Movie();
		editMovie.setMovieId(15);
		editMovie.setMovieName("New Edit Movie 2");
		editMovie.setMovieBanner("New Edit Movie Banner");
		editMovie.setReleaseDate("19/19/2011 invalid date field");
		editMovie.setRent(20);
		editMovie.setCategoryid(1);
		editMovie.setAvailableCopy(10);
		editMovie.setTotalCopy(10);
		editMovie.setNo_of_ratings(0);
		editMovie.setRating(0);
		
		moviedao bussObj = new moviedao();
		try
		{
			isValid = bussObj.updateMovie(editMovie);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());		
		}
		catch(MovieAlreadyExistsException ex1)
		{
			System.out.println(ex1.toString());
		}
		catch(InvalidEnteriesException ex2)
		{
			System.out.println(ex2.toString());
		}
		Assert.assertEquals(0, isValid);
	}
	
	/*
	 * Edit a movie which has the same name and banner as the other movie in the database!
	 * It will throw exception: MovieAlreadyExistsException
	 */
	public void testUpdateMovie_inValid_2() {

		int isValid = 0;
		
		Movie editMovie = new Movie();
		editMovie.setMovieId(15);
		editMovie.setMovieName("Immortals");
		editMovie.setMovieBanner("Dir: Tarsem Singh With: Henry ");
		editMovie.setReleaseDate("2011-10-06");
		editMovie.setRent(20);
		editMovie.setCategoryid(1);
		editMovie.setAvailableCopy(10);
		editMovie.setTotalCopy(10);
		editMovie.setNo_of_ratings(0);
		editMovie.setRating(0);
		
		moviedao bussObj = new moviedao();
		try
		{
			isValid = bussObj.createMovie(editMovie);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());		
		}
		catch(MovieAlreadyExistsException ex1)
		{
			System.out.println(ex1.toString());
		}
		catch(InvalidEnteriesException ex2)
		{
			System.out.println(ex2.toString());
		}
		Assert.assertEquals(0, isValid);
	}

	/*
	 * Issue movie valid movieId and all valid arguments
	 */
	public void testIssueMovie_valid() 
	{
		int isValid = 0;
		int MovieId = 1;
		long MembershipId = 1;
		moviedao bussObj = new moviedao();
		try
		{
			isValid = bussObj.issueMovie(MovieId, MembershipId);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());	
		}
		catch(MovieUnavailableException munEx)
		{
			System.out.println(munEx.toString());
		}
		catch(UserNotEligibleException uneEx)
		{
			System.out.println(uneEx.toString());
		}
		catch(MovieAlreadyIssuedException madEx)
		{
			System.out.println(madEx.toString());
		}
		catch(MovieNotExistsException ex)
		{
			System.out.println(ex.toString());
		}
		Assert.assertEquals(1, isValid);
	}
	
	/*
	 * Issue movie which doesn't has Available copies
	 * It will show MovieUnavailableException
	 */
	public void testIssueMovie_inValid_1() 
	{
		int isValid = 0;
		int MovieId = 15;
		long MembershipId = 100000000;
		moviedao bussObj = new moviedao();
		try
		{
			isValid = bussObj.issueMovie(MovieId, MembershipId);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());	
		}
		catch(MovieUnavailableException munEx)
		{
			System.out.println(munEx.toString());
		}
		catch(UserNotEligibleException uneEx)
		{
			System.out.println(uneEx.toString());
		}
		catch(MovieAlreadyIssuedException madEx)
		{
			System.out.println(madEx.toString());
		}
		catch(MovieNotExistsException ex)
		{
			System.out.println(ex.toString());
		}
		Assert.assertEquals(0, isValid);
	}
	
	/*
	 * Issue movie invalid movieId or movie which is already deleted!
	 * It will throw: MovieNotExistsException
	 */
	public void testIssueMovie_inValid_2() 
	{
		int isValid = 0;
		int MovieId = 1000011111;
		long MembershipId = 100000000;
		moviedao bussObj = new moviedao();
		try
		{
			isValid = bussObj.issueMovie(MovieId, MembershipId);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());	
		}
		catch(MovieUnavailableException munEx)
		{
			System.out.println(munEx.toString());
		}
		catch(UserNotEligibleException uneEx)
		{
			System.out.println(uneEx.toString());
		}
		catch(MovieAlreadyIssuedException madEx)
		{
			System.out.println(madEx.toString());
		}
		catch(MovieNotExistsException ex)
		{
			System.out.println(ex.toString());
		}
		Assert.assertEquals(0, isValid);
	}
	
	/*
	 * Delete movie with movie id as random number between 0 and 999
	 * If movie is already deleted, it will throw MovieNotExistsException
	 * otherwise it will perform delete operation.
	 * Also if movie has pending issued movies, it will return pendingIssuedMoviesException!
	 */
	public void testDeleteMovie_1() 
	{
		int isValid = 0;
		int movieId = new Random().nextInt(1000);
		
		moviedao bussObj = new moviedao();
		try
		{
			isValid = bussObj.deleteMovie(movieId);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());	
		}
		catch(PendingIssuedMoviesException pmex)
		{
			System.out.println(pmex.toString());
		}
		catch(MovieNotExistsException mnotexistex)
		{
			System.out.println(mnotexistex.toString());
		}
		
	}
	
	/*
	 * Delete the movies with pending issued movies
	 */
	public void testDeleteMovie_2() 
	{
		int isValid = 0;
		int movieId = 1;
		
		moviedao bussObj = new moviedao();
		try
		{
			isValid = bussObj.deleteMovie(movieId);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());	
		}
		catch(PendingIssuedMoviesException pmex)
		{
			System.out.println(pmex.toString());
		}
		catch(MovieNotExistsException mnotexistex)
		{
			System.out.println(mnotexistex.toString());
		}
		Assert.assertEquals(0, isValid);
	}



	
	/*
	 * Generate bill for the simple customer!
	 */
	public void testGenerateBillSimple_1() 
	{
		Double Balance = 0.0;
		Double Result = 16.0;
		
		Movie[] movieList = null;
		
		Movie a = new Movie();
		Movie b = new Movie();
		a.setMovieId(1);
		b.setMovieId(2);
		a.setRent(10.0);
		b.setRent(6.0);
		
		movieList = new Movie[2];
		movieList[0] = a;
		movieList[1] = b;
		
		long membershipId = 1;
		moviedao bussObj = new moviedao();
		
		try
		{
			Balance = bussObj.generateBillSimple(membershipId,movieList);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());
		}
		Assert.assertEquals(Result, Balance);
	}

	public void testGenerateBillPremium() 
	{
		Movie[] movieList= null;
	
		long membershipId = 1;
		moviedao bussObj = new moviedao();
		
		try
		{
			movieList = bussObj.generateBillPremium(membershipId);
			System.out.println("In generateBillPremium :"+movieList.length);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());
		}
		Assert.assertEquals(1, movieList[0].getMovieId());
	}

	
	public void testGetMovieById_valid() 
	{
		Movie m = null;
		moviedao bussObj = new moviedao();
		int movieId = 1;
		try
		{
		m = bussObj.getMovieById(movieId);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());
		}
		catch(MovieNotExistsException mnotex)
		{
			System.out.println(mnotex.toString());
		}
		Assert.assertEquals(1, m.getMovieId());
	}
	
	/*
	 * If the movie does not exists in the database!
	 * It will throw movie does not exists exception!
	 */
	public void testGetMovieById_inValid() 
	{
		Movie m = null;
		moviedao bussObj = new moviedao();
		int movieId = 1100011019;
		try
		{
		m = bussObj.getMovieById(movieId);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());
		}
		catch(MovieNotExistsException mnotex)
		{
			System.out.println(mnotex.toString());
		}
	}

	/*
	 * movie is already issued to the user!
	 */
	public void testMovieAlreadyIssued_1() 
	{
		int result =0;
		moviedao bussObj = new moviedao();
		int movieId = 1;
		long membershipId = 1;
		
		try
		{
		result = bussObj.movieAlreadyIssued(movieId,membershipId);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());
		}
		Assert.assertEquals(0, result);
	}
	
	/*
	 * movie is already issued to the user!
	 * In this case movie is not issued to the user.
	 * So will return 0
	 */
	public void testMovieAlreadyIssued() 
	{
		int result =0;
		moviedao bussObj = new moviedao();
		int movieId = 10;
		long membershipId = 1;
		
		try
		{
		result = bussObj.movieAlreadyIssued(movieId,membershipId);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());
		}
	}

	/*
	 * test if the movie is available or not!
	 */
	public void testIsMovieAvailable() 
	{
		int isAvailable = 0;
		moviedao bussObj = new moviedao();
		int movieId = 1;
		
		try
		{
		isAvailable = bussObj.isMovieAvailable(movieId);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());
		}
		//catch(MovieUnavailableException munex)
		//{
			//System.out.println(munex.toString());
		//}
		Assert.assertEquals(1, isAvailable);
	}

	
	/*
	 * user submits the movie
	 */
	public void testSubmitMovie_valid() 
	{
			int isValid = 0;
			int movieId = 1;
			long membershipId = 1;
			moviedao bussObj = new moviedao();
			try
			{
				isValid = bussObj.submitMovie(movieId, membershipId);
			}
			catch(ServiceUnavailableException suex)
			{
				System.out.println(suex.toString());	
			}
			catch(MovieNotIssuedtoUserException munex)
			{
				System.out.println(munex.toString());
			}
			Assert.assertEquals(1, isValid);
	}

	/*
	 * user submits the movie, but user has not issued that movie!
	 */
	public void testSubmitMovie_inValid() 
	{
			int isValid = 0;
			int movieId = 1;
			long membershipId = 100000011;
			moviedao bussObj = new moviedao();
			try
			{
				isValid = bussObj.submitMovie(movieId, membershipId);
			}
			catch(ServiceUnavailableException suex)
			{
				System.out.println(suex.toString());	
			}
			catch(MovieNotIssuedtoUserException munex)
			{
				System.out.println(munex.toString());
			}
			Assert.assertEquals(0, isValid);
	}
	
	public void testIsUserEligible() 
	{
		int isEligible = 0;
		long membershipId = 1;
		moviedao bussObj = new moviedao();
		try
		{
			isEligible = bussObj.isUserEligible(membershipId, 1);
		}catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());	
		}
		//catch(UserNotEligibleException notex)
		//{
			//System.out.println(notex.toString());	
		//}
		Assert.assertEquals(1, isEligible);
	}

	public void testGetUserIssuedMovies() 
	{
		Movie[] movieList= null;
		moviedao bussObj = new moviedao();
		try
		{
			movieList = bussObj.getUserIssuedMovies(1);
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());
		}
		catch(UserHasNotIssuedAnyMovieException uhnimex)
		{
			System.out.println(uhnimex.toString());
		}
	}
	
	/*
	 * This will test the GetAllCatIds() function.
	 * It will return all the category Ids from the database!
	 */
	
	public void testGetAllCatIds() 
	{
		int[] catagoryIds = null;
		MovieBussiness bussObj = new MovieBussiness();
		try
		{
			catagoryIds = bussObj.getAllCatIds();
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println(suex.toString());
		}
	}


	/*
	 *  This will return all the movies!
	 */
	public void testGetAllMovies() 
	{
		Movie[] movieList= null;
		
		MovieBussiness bussObj = new MovieBussiness();
		try
		{
			movieList = bussObj.getAllMovies();
		}
		catch(ServiceUnavailableException suex)
		{
			System.out.println(suex.toString());
		}	
	}
}
